﻿/*
	* Project	: SharePoint Dynamic Forms
	* Version	: 1.0
	* Company	: SharePoint Frontier Software Pvt Ltd. (http://www.sharepointfrontier.com)
	* Author	: Ashok Raja .T (Blog : http://www.ashokraja.me)
	* Licence	: Published under Microsoft Public License (Ms-PL)
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SFS.SharePoint.DynamicForms.Validation
{
    public class DateValidator
    {
        public static bool GreaterThan(SPDynamicFieldValidationRule rule, SPDynamicField Field)
        {
            return Validate(rule, Field, "g");
        }

        public static bool GreaterThanEqual(SPDynamicFieldValidationRule rule, SPDynamicField Field)
        {
            return Validate(rule, Field, "ge");
        }

        public static bool LessThan(SPDynamicFieldValidationRule rule, SPDynamicField Field)
        {
            return Validate(rule, Field, "l");
        }

        public static bool LessThanEqual(SPDynamicFieldValidationRule rule, SPDynamicField Field)
        {
            return Validate(rule, Field, "le");
        }

        public static bool Equal(SPDynamicFieldValidationRule rule, SPDynamicField Field)
        {
            return Validate(rule, Field, "e");
        }

        public static bool NotEqual(SPDynamicFieldValidationRule rule, SPDynamicField Field)
        {
            return Validate(rule, Field, "ne");
        }

        private static bool Validate(SPDynamicFieldValidationRule rule, SPDynamicField Field, string Operator)
        {
            try
            {
                DateTime FieldValue = DateTime.MinValue;
                DateTime RuleValue = DateTime.MinValue;

                if (!DateTime.TryParse(Field.Value.ToString(), out FieldValue))
                {
                    rule.ValidationMessage = Field.DisplayName + " is not a valid date.";
                    rule.IsValid = false;
                    return false;
                }

                FieldValue = FieldValue.Date;
               
                try
                {
                    RuleValue = ValidationHelper.GetDateFromToken(rule.ValidateAgainst).Date;
                }
                catch  
                {
                    rule.ValidationMessage = rule.ValidationType + " validation rule is invalid for " + Field.DisplayName + ".";
                    rule.IsInvalidRule = true;
                    rule.IsValid = false;
                    return false;
                }

                //if (rule.ValidateAgainst.Substring(0, 7).ToUpper() != "[TODAY]" && !DateTime.TryParse(rule.ValidateAgainst, out RuleValue))
                //{
                //    rule.ValidationMessage = rule.ValidationType + " validation rule is invalid for " + Field.DisplayName + ".";
                //    rule.IsValid = false;
                //    return false;
                //}

                //FieldValue = FieldValue.Date;
                //RuleValue = RuleValue.Date;

                //if (rule.ValidateAgainst.Substring(0, 7).ToUpper() == "[TODAY]")
                //{
                //    RuleValue = DateTime.Now.Date;
                //    if (rule.ValidateAgainst.Length > 8)
                //    {
                //        string DateOperator = rule.ValidateAgainst.Substring(7, 1);
                //        if (DateOperator == "+" || DateOperator == "-")
                //        {
                //            string PeriodType = rule.ValidateAgainst.Substring(rule.ValidateAgainst.Length - 1);
                //            int Period = int.Parse(rule.ValidateAgainst.Substring(8, rule.ValidateAgainst.Length - 9));

                //            switch (PeriodType)
                //            {
                //                case "d":
                //                    RuleValue = DateOperator == "+" ? RuleValue.AddDays(Period) : RuleValue.AddDays(Period * -1);
                //                    break;
                //                case "m":
                //                    RuleValue = DateOperator == "+" ? RuleValue.AddMonths(Period) : RuleValue.AddMonths(Period * -1);
                //                    break;
                //                case "y":
                //                    RuleValue = DateOperator == "+" ? RuleValue.AddYears(Period) : RuleValue.AddYears(Period * -1);
                //                    break;
                //                default:
                //                    break;
                //            }
                //        }
                //        else
                //        {
                //            rule.ValidationMessage = rule.ValidationType + " validation rule is invalid for " + Field.DisplayName + ".";
                //            rule.IsValid = false;
                //            return false;
                //        }
                //    }
                //}


                string DisplayString = RuleValue.ToString("dd-MMM-yyyy");
                switch (Operator)
                {
                    case "g":
                        if (FieldValue > RuleValue) { return true; }
                        else
                        {
                            rule.ValidationMessage = ValidationHelper.ProcessErrorMsg(Field, rule, Field.DisplayName + " must be greater than " + DisplayString + ".");
                            rule.IsValid = false;
                            return false;
                        }
                    case "ge":
                        if (FieldValue >= RuleValue) { return true; }
                        else
                        {
                            rule.ValidationMessage = ValidationHelper.ProcessErrorMsg(Field, rule, Field.DisplayName + " must be greater than or equal " + DisplayString + ".");
                            rule.IsValid = false;
                            return false;
                        }

                    case "l":
                        if (FieldValue < RuleValue) { return true; }
                        else
                        {
                            rule.ValidationMessage = ValidationHelper.ProcessErrorMsg(Field, rule, Field.DisplayName + " must be less than " + DisplayString + ".");
                            rule.IsValid = false;
                            return false;
                        }
                    case "le":
                        if (FieldValue <= RuleValue) { return true; }
                        else
                        {
                            rule.ValidationMessage = ValidationHelper.ProcessErrorMsg(Field, rule, Field.DisplayName + " must be less than " + DisplayString + ".");
                            rule.IsValid = false;
                            return false;
                        }
                    case "e":
                        if (FieldValue == RuleValue) { return true; }
                        {
                            rule.ValidationMessage = ValidationHelper.ProcessErrorMsg(Field, rule, Field.DisplayName + " must be equal to " + DisplayString + ".");
                            rule.IsValid = false;
                            return false;
                        }
                    case "ne":
                        if (FieldValue != RuleValue) { return true; }
                        {
                            rule.ValidationMessage = ValidationHelper.ProcessErrorMsg(Field, rule, Field.DisplayName + " must not be equal to " + DisplayString + ".");
                            rule.IsValid = false;
                            return false;
                        }
                }
                return true;
            }
            catch
            {
                rule.ValidationMessage = "Invalid input data.";
                rule.IsValid = false;
                return false;
            }
        }


    }
}

